package com.xcx.service.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xcx.dto.StoreInspectionRecordDto;
import com.xcx.dto.StoreInspectionRecordPages;
import com.xcx.entity.StoreInspectionRecord;
import com.xcx.entity.StoreInspectionRecordExcel;
import com.xcx.exception.BaseException;
import com.xcx.mapper.StoreInspectionRecordMapper;
import com.xcx.result.PageResult;
import com.xcx.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mwf
 */
@Slf4j
@Service
public class StoreInspectionRecordServiceImpl extends ServiceImpl<StoreInspectionRecordMapper, StoreInspectionRecord> {

    @Autowired
    private StoreInspectionRecordMapper storeInspectionRecordMapper;

    public void addRecord(StoreInspectionRecordDto storeInspectionRecordDto) {
        StoreInspectionRecord storeInspectionRecord = new StoreInspectionRecord();
        BeanUtils.copyProperties(storeInspectionRecordDto, storeInspectionRecord);
        storeInspectionRecord.setCreateDate(LocalDateTime.now());
        int insert = storeInspectionRecordMapper.insert(storeInspectionRecord);
        if (insert != 1) throw new BaseException("添加失败");
    }

    public PageResult queryRecord(StoreInspectionRecordPages storeInspectionRecordPages) {
        PageHelper.startPage(storeInspectionRecordPages.getPage(), storeInspectionRecordPages.getPageSize());
        Page<StoreInspectionRecordExcel> page = storeInspectionRecordMapper.pageQuery(storeInspectionRecordPages);
        return new PageResult(page.getTotal(), page.getResult());
    }


    public void exportRecord(HttpServletResponse response, StoreInspectionRecordPages storeInspectionRecordPages) throws Exception {
        List<StoreInspectionRecordExcel> result = storeInspectionRecordMapper.pageQuery(storeInspectionRecordPages).getResult();
        ExcelUtils.writeExcel(response, result, "打分记录", "打分记录", StoreInspectionRecordExcel.class);
    }
}
